using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using System.Web;
using Criptografia;
namespace ControlSesion
{

    public class VariableSesion
    {

        private const string ID__USER = "__Ui";
        private const string USER__NAME = "__UN";
        private const string __TEMA = "__TE";

        private static int index = HttpContext.Current.Server.MapPath("~/").Split('\\').Length;
        //Private Shared COOKIE__PATH As String = "/" + HttpContext.Current.Server.MapPath("~/").Split("\")(index - 2)

        private static string COOKIE__PATH = "/";

        private const int MESES__COOKIES = 24;

        #region "Cookies"


        public static int Id_User
        {
            get
            {
                if ((HttpContext.Current.Request.Cookies[ID__USER] != null))
                {
                    return int.Parse(Criptografia.Criptografia.DesEncripta(HttpContext.Current.Request.Cookies[ID__USER].Value.ToString()));
                }
                return 0;
            }
        }

        public static string User_Name
        {
            get
            {
                if ((HttpContext.Current.Request.Cookies[USER__NAME] != null))
                {
                    return Criptografia.Criptografia.DesEncripta(HttpContext.Current.Request.Cookies[USER__NAME].Value.ToString());
                }
                return null;
            }
        }

        public static string Tema
        {
            get
            {
                if ((HttpContext.Current.Request.Cookies[__TEMA] != null))
                {
                    return Criptografia.Criptografia.DesEncripta(HttpContext.Current.Request.Cookies[__TEMA].Value.ToString());
                }
                return null;
            }

            set
            {
                string elTema = "miTema" + value.ToString();
                guardarCookie(__TEMA, elTema, false);
            }
        }



        #endregion

        #region "Funciones cookies"

        public static void EliminarCookies()
        {
            EliminarLaCookie(ID__USER);
            EliminarLaCookie(USER__NAME);
        }

        public static void EliminarLaCookie(string cookie)
        {
            //if ((HttpContext.Current.Request.Cookies[cookie] != null)) {
            //    HttpCookie lacookie = null;
            //    lacookie = new HttpCookie(cookie, "123");
            //    lacookie.Expires = System.DateTime.Now.AddMonths(-2);
            //}
            if ((HttpContext.Current.Request.Cookies[cookie] != null))
            {
                HttpCookie lacoocookie = null;
                lacoocookie = new HttpCookie(cookie, "");
                lacoocookie.Expires = System.DateTime.Now.AddMonths(-2);
                lacoocookie.Path = COOKIE__PATH;
                HttpContext.Current.Response.AppendCookie(lacoocookie);
            }
        }

        public static void Desloguearse()
        {
            EliminarCookies();
        }

        public static void LoguearCoockie(int userId, string userName, bool recordar)
        {
            guardarCookie(ID__USER, userId.ToString(), recordar);
            guardarCookie(USER__NAME, userName, recordar);
        }

        private static void guardarCookie(string nombre, string valor, bool recordar)
        {
            HttpCookie cookie = null;
            cookie = new HttpCookie(nombre, Criptografia.Criptografia.Encripta(valor));
            if (recordar)
            {
                cookie.Expires = System.DateTime.Now.AddMonths(MESES__COOKIES);
            }
            cookie.Path = COOKIE__PATH;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        #endregion


    }
}
